package com.lenovo.smarttraffic.Test;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lenovo.smarttraffic.R;
import com.lenovo.smarttraffic.zm.OkHttpUtils1;

import org.json.JSONObject;

import java.io.IOException;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;


public class TestActivity extends AppCompatActivity {
    String TAG = "TestActivity";
    String url = "http://192.168.1.110:8088/transportservice/action/GetCarParkInfo.do";
    private TextView tv_okHttp;

    String s = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
        initView();
//        Retrofit retrofit = new Retrofit.Builder()
//                .baseUrl("http://fanyi.youdao.com/") // 设置 网络请求 Url
//                .build();
//        PostRequest_Interface request = retrofit.create(PostRequest_Interface.class);
//        Call<ResponseBody> call = request.getCall("I love you");
//
//        //步骤6:发送网络请求(异步)
//        call.enqueue(new Callback<ResponseBody>() {
//
//            //请求成功时回调
//            @Override
//            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
//                // 步骤7：处理返回的数据结果：输出翻译的内容
//                try {
//                    String s =  response.body().string();
//                    Log.e(TAG, "onResponse: " + s);
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
//
//            }
//
//            //请求失败时回调
//            @Override
//            public void onFailure(Call<ResponseBody> call, Throwable throwable) {
//                System.out.println("请求失败");
//                System.out.println(throwable.getMessage());
//            }
//        });

        TestBean testBean = new TestBean("user1");
        Retrofit retrofit = new Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create())
                .baseUrl("http://192.168.1.110:8088/transportservice/action/") // 设置 网络请求 Url
                .build();
        PostRequest_Interface request = retrofit.create(PostRequest_Interface.class);
        Call<ResponseBody> call = request.getCall(testBean);

        //步骤6:发送网络请求(异步)
        call.enqueue(new Callback<ResponseBody>() {

            //请求成功时回调
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                // 步骤7：处理返回的数据结果：输出翻译的内容
                try {
                    Boolean ssss = null;
                    String s = response.body().string();

                    Log.e(TAG, "onResponse: " + s);
                    tv_okHttp.setText(s);
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }

            //请求失败时回调
            @Override
            public void onFailure(Call<ResponseBody> call, Throwable throwable) {
                System.out.println("请求失败");
                System.out.println(throwable.getMessage());
            }
        });
    }

    private void initView() {
        tv_okHttp = (TextView) findViewById(R.id.tv_okHttp);

    }
}
